package com.kkwrite.regimen.provider.service.picture;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.kkwrite.regimen.common.entity.dto.picture.PictureDTO;
import com.kkwrite.regimen.provider.domain.RegPictureDO;
import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.repository.RegPictureRepository;
import com.kkwrite.regimen.provider.service.BaseServiceImpl;

/** 
 * 图片服务
 *
 * @author Soosky Wang
 * @date 2018年7月21日 上午10:27:21 
 * @version 1.0.0
 */
@Service("pictureService")
public class PictureServiceImpl extends BaseServiceImpl implements PictureService {

	private static final Logger logger = LoggerFactory.getLogger(PictureServiceImpl.class);

	@Resource(name = "regPictureRepository")
	private RegPictureRepository regPictureRepository;
	
	/**
	 * 获取 OwerId 的配图
	 * @param owerId
	 * @return
	 */
	@Override
	public List<PictureDTO> getOwerPictures(Long owerId, Short owerType) throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("PictureServiceImpl.getOwerIdPictures(), owerId = {}", owerId);
		}
		
		try {
			// 查询属主配图
			List<RegPictureDO> pictureDOs = regPictureRepository.findByPictureOwnerIdAndPictureOwnerType(owerId, owerType);
			if (pictureDOs == null) {
				return null;
			}
			
			List<PictureDTO> pictureDTOs = new ArrayList<>();
			for (RegPictureDO pictureDO: pictureDOs) {
				PictureDTO pictureDTO = new PictureDTO();
				BeanUtils.copyProperties(pictureDO, pictureDTO);
				pictureDTOs.add(pictureDTO);
			}
			
			return pictureDTOs;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	
}
